package com.sunday.shoufen.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.shoufen.R;
import com.sunday.shoufen.http.ApiClient;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/11/7.
 */

public class ShopDescActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.webview)
    WebView webview;

    private String html;
    private long shopId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_desc);
        ButterKnife.bind(this);
        html = getIntent().getStringExtra("html");
        shopId = getIntent().getLongExtra("shopId", 0);
        titleView.setText("商铺简介");

        WebSettings webSettings = webview.getSettings();
        //设置WebView属性，能够执行Javascript脚本
        webSettings.setJavaScriptEnabled(true);
        //设置可以访问文件
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        //设置支持缩放
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setDisplayZoomControls(false);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);


        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
//        int fontSize = (int) getResources().getDimension(R.dimen.s15);
//        Log.i(TAG, "initView: fontSize = " + fontSize);
//        webSettings.setDefaultFontSize(fontSize);
        String url = ApiClient.API_URL + "/mobile/shop/getShopDescHtml?shopId=" + shopId;
        //加载需要显示的网页
        //webview.loadData(html,"text/html", "uft-8");
        // webview.loadData(html, "text/html; charset=UTF-8", null);//这种写法可以正确解码
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (!TextUtils.isEmpty(url)) {
                    view.loadUrl(url);
                } else {
                    dissMissDialog();
                }
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                dissMissDialog();
            }
        });
        webview.loadUrl(url);
        webview.setWebChromeClient(new WebChromeClient());
    }


}
